import { Message } from 'element-ui'

// 根据屏幕自适应table高度
// obj.$refs[elTableRefName].$el.offsetTop 表格距离视口顶部的高度
function setTableHeight (obj, tableHeight, elTableRefName) {
    if (obj.$refs[elTableRefName] && obj.$refs[elTableRefName].$el) {
        obj.$nextTick(() => {
            // 减去60 给分页组件留出位置
            obj[tableHeight] = window.innerHeight - obj.$refs[elTableRefName].$el.offsetTop - 90
            window.onresize = () => {
                // console.log(window.innerHeight)
                if (obj.$refs[elTableRefName] && obj.$refs[elTableRefName].$el) {
                    obj[tableHeight] = window.innerHeight - obj.$refs[elTableRefName].$el.offsetTop - 90
                }
            }
        })
    }
}

// 根据屏幕自适应table高度 无分页条表格
// obj.$refs[elTableRefName].$el.offsetTop 表格距离视口顶部的高度
function setTableHeightNoPagination (obj, tableHeight, elTableRefName) {
    if (obj.$refs[elTableRefName] && obj.$refs[elTableRefName].$el) {
        obj.$nextTick(() => {
            // 减去60 给分页组件留出位置
            obj[tableHeight] = window.innerHeight - obj.$refs[elTableRefName].$el.offsetTop
            window.onresize = () => {
                // console.log(window.innerHeight)
                if (obj.$refs[elTableRefName] && obj.$refs[elTableRefName].$el) {
                    obj[tableHeight] = window.innerHeight - obj.$refs[elTableRefName].$el.offsetTop
                }
            }
        })
    }
}

// 获得当前日期
function getNowDate (time) {
    let date
    if (time) {
        date = new Date(time)
    } else {
        date = new Date()
    }
    let year = date.getFullYear().toString()
    let month = (date.getMonth() + 1).toString().padStart(2, 0)
    let day = date.getDate().toString().padStart(2, 0)
    let h = date.getHours().toString().padStart(2, 0)
    let min = date.getMinutes().toString().padStart(2, 0)
    let s = date.getSeconds().toString().padStart(2, 0)
    return `${year}-${month}-${day} ${h}:${min}:${s}`
}

// 计算选择的日期的间隔天数 结束时间是否大于初始时间 是则返回true
// startTime:'2022-6-30 12:12:25'
function dateDiff (startTime, endTime) {
    if (!startTime || !endTime) {
        return true
    }
    if (typeof startTime != 'string' || typeof endTime != 'string') {
        return Message.warning('必须传入类型为字符串的时间格式')
    }
    let d1 = Date.parse(new Date(startTime))
    let d2 = Date.parse(new Date(endTime))
    // d 间隔天数
    const d = parseInt(d2 - d1) / (1000 * 60 * 60 * 24)
    if (d >= 0) {
        return true
    }
    else {
        Message.warning('结束日期必须大于开始日期')
        return false
    }
}

export { setTableHeight, setTableHeightNoPagination, getNowDate, dateDiff }